package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;
import com.xxxx.yoyo.util.PageUtil;

/**
 * Servlet implementation class HotServlet
 */
@WebServlet("/index/top")
public class TopServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	private IGoodsService goodsService = new GoodsServiceImpl();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取前端传递的参数
		String typeIdStr = request.getParameter("typeid");//类型的id
		String pageStr = request.getParameter("page");//当前页码
		String sizeStr = request.getParameter("size");//每页条数
		//把字符串转换为整型
		int typeId = typeIdStr == null ? 2 : Integer.parseInt(typeIdStr);//如果没有获取到typeid, 默认选中热销
		int page = pageStr == null ? 1 : Integer.parseInt(pageStr);//如果没有获取到page, 默认第1页
		int size = sizeStr == null ? 16 : Integer.parseInt(sizeStr);//如果没有获取到size,默认一页显示16条
		
		//设置导航条上的 [热销]/[新品] 处于选中状态
		request.setAttribute("flag", typeId + 5);
		
		//获取 [宠物分类] 列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request中, 供/index/goods.jsp使用
		request.setAttribute("typeList", typeList);
		
		//设置内容区域标题是 [热销]/[新品]   2表示热销推荐 3表示新品推荐 4表示搜索结果
		request.setAttribute("typeid", typeId);
		
		//根据榜单类型,查询宠物列表  1表示今日推荐 2表示热销推荐 3表示新品推荐
		List<Goods> goodsList = goodsService.getGoodsListByHotType(typeId, page, size);
		//把goodsList存入request中, 供/index/goods.jsp使用
		request.setAttribute("goodList", goodsList);
		
		//查询 [热销]/[新品] 对应的宠物条数
		long count = goodsService.getGoodsCountByHotType(typeId);
		
		//获取分页信息
		String pageInfo = PageUtil.getPageTool(request, count, page, size);
		//把pageInfo存入request中, 供/index/goods.jsp使用
		request.setAttribute("pageTool", pageInfo);
		
		//跳转到宠物列表页面 /index/goods.jsp
		request.getRequestDispatcher("/index/goods.jsp").forward(request, response);
	}

}
